<?php
/**===========================================================================
 * 작성자	: 하성수
 * 용도		: ActionClass 사용시 다양한 컨트롤을 가능하게 해줍니다.
 * 주의		: $controllerPath 정보를 서버환경에 맞추어 수정하여 사용합니다.
 * 연락처	: spacer.ha@gmail.com
 * 사용제한 : 
	- 이 클래스 라이브러리 사용은 무료이나, 원작자의 정보나 주석을 삭제할 수 없습니다.(자동생성되는 뷰캐시내 주석 포함)
	- 원작자가 배포한 그대로 수정되지 않은 소스는 자유롭게 배포가 가능합니다.
	- 사용자환경에 맞게 소스를 수정하여 사용은 가능하나 수정된 소스나, 수정된 소스가 포함된 프로그램을 타인에게 제공할 수 없습니다.
	- 영리/비영리/개인/기업 관계없이 모두 사용할 수 있습니다.
 * 버전     : 11.7.1
============================================================================*/
class Controller {

	var $ver						= '11.7.1';
	var $controllerPath				= '../Spac/controller';
	var $actionClassPath;
	var $actionClassName;
	var $actionMethodName;
	var $userConfig;
	var $cacheFileName;
	var $cacheFilePath;
	var $classFileExt				= "Class.php";
	var $defaultMethodName			= "execute";
	var $result_uri					= '';
	var $result_full_uri			= '';
	var $page_move_type				= 'none';
	var $layoutTitle				= '';
	var $iniActionUriMapPath;
	var $iniActionClassMapPath;
	var $viewCachePrint				= false;
	var $iniControllerName			= 'Controller';
	var $iniControllerPath;
	var $iniLayoutPath;
	var $layout						= null;
	var $iniViewTemplateFilePath;
	var $viewTemplateObject			= null;
	var $iniCryptName				= 'Crypt';
	var $iniCryptPath				= '';
	var $cryptConfig				= array();
	var $cryptObject				= null;
	var $loadControllerConfig		= false;

	var $baseUrl					= "/hello/www";
	var $baseClass					= "/hello/www/index.php";

	function Controller() {
		$this->initController();
	}
	function initController($iniControllerPath = "") {
		
		if($iniControllerPath) {
			$this->iniControllerPath = $iniControllerPath;
		}
		$conf_controller 	= array();
		if(!$this->iniControllerPath) {
			$this->iniControllerPath		= $this->controllerPath."/".$this->iniControllerName.".ini";
		}
		if(!file_exists($this->iniControllerPath)) {
			$this->iniControllerFileError();
			exit;
		}
		$conf_controller			= parse_ini_file($this->iniControllerPath);
		foreach($conf_controller as $key => $value) {
			$this->{$key}			= $value;
		}
		if($this->useCryptUrl == true) {
			$this->setCryptIniConfig();
		}		
		$this->loadControllerConfig	= true;

		// Custom setting for session
		session_start();
	}
	function getControllerInstance($ClassName) {
		require_once($this->controllerPath."/".$ClassName."/".$ClassName .".".$this->classFileExt);
		$this->{$ClassName} = new $ClassName;
		$this->requestClass = $ClassName;
	}
	function getModelInstance($ClassName) {
		require_once($this->modelPath."/".$ClassName .".".$this->classFileExt);
		$this->{$ClassName} = new $ClassName;
		$this->requestClass = $ClassName;
	}
	function getUserControllerInstance($ClassPath,$ClassFileName) {
		require_once($this->controllerPath."/".$ClassPath."/".$ClassFileName);
	}
	function setUserIniConfig($userIniFilePath) {
		if(!file_exists($userIniFilePath)) {
			$this->ConfigFileError($userIniFilePath);
			exit;
		}
		$iniVarArray			= parse_ini_file($userIniFilePath);
		$this->userConfig		= (object)$iniVarArray;
	}
	function setCryptIniConfig() {				
		if(!file_exists($this->iniCryptPath)) {
			$this->ConfigFileError($this->iniCryptPath);
			exit;
		}
		$iniVarArray			= parse_ini_file($this->iniCryptPath);
		$this->cryptConfig		= (object)$iniVarArray;		
	}
	function dispatcher() {
		if($this->page_move_type == 'dispatcherLayout') {
			$this->dispatcherLayout();				
		} else {
			$this->page_move_type	= 'dispatcher';				
			$this->checkResultUri();
			if(!$this->result_uri) {
				$this->viewHtmlErrorPage("뷰파일이(result_uri) 정의되어있지 않습니다.");
				exit;					
			}
			$this->dispatcherCache();				
			if($this->viewCachePrint == false) {
				if(file_exists($this->viewPath.$this->result_uri)) {
					$Data = file_get_contents($this->viewPath.$this->result_uri);
				} else {
					$this->NoFileError("dispatcher");
					exit;
				}
				if($this->useTemplateTag == true) {
					$Data = $this->applyViewTemplate($Data);
				}
				if($this->useViewCache == true) $this->writeCache($Data);
				if($this->viewDispatcherSource == true) {
					highlight_string($Data,false);
				} else {
					eval("?>".$Data);
				}
				if($this->viewCacheInfo == true && $this->useViewCache == true) {
					$this->viewCacheMsg(0);
				}
			}				
		}
	}
	function dispatcherCache() {
		$this->viewCachePrint = false;
		if($this->result_uri != $this->errPage) {
			if($this->useViewCache == true) {
				if($this->isUpdateCache() == false) {
					$Data = file_get_contents($this->cacheFilePath);						
					if(!$this->viewDispatcherSource) {
						eval("?>".$Data);
					} else {
						highlight_string($Data,false);
					}
					if($this->viewCacheInfo == true) {
						$this->viewCacheMsg(1);
					}
					$this->viewCachePrint = true;
				}				
			}				
		}			
	}
	function isUpdateCache() {
		$result = false;
		$this->setCachePath();
		if(!file_exists($this->cacheFilePath)) {
			$result = true;				
		} else {
			if($this->autoViewCacheUpdate == true) {					
				$cacheCreatTime 	= date("YmdHis", filemtime($this->cacheFilePath));
				if(file_exists($this->actionClassPath)) {
					$resultCreateTime	= date("YmdHis", filemtime($this->actionClassPath));
					if($cacheCreatTime < $resultCreateTime) {
						$result = true;
					}
				}
				if($this->page_move_type == 'dispatcherLayout') {						
					if(!$this->targetLayoutName) {
						$this->targetLayoutName = "body";
					}						
					$true_count = 0;
					foreach($this->layout as $key => $value) {
						$loopViewFilePath = $this->viewPath.$value;
						if($key == $this->targetLayoutName) {
							if(!$this->result_uri) {
								if($value) {
									$this->result_uri = $value;
								} else {
									$this->viewHtmlErrorPage("레이아웃 환경설정파일(Layout.ini)의 '[".$this->layoutTitle."]' 레이아웃 항목 중 '".$this->targetLayoutName."' 항목의 뷰파일명을 정의하시거나 result_uri를 지정해주세요");
									exit;									
								}
							}
							$loopViewFilePath = $this->viewPath.$this->result_uri;
						}
						if(!file_exists($loopViewFilePath)) {
							$this->viewHtmlErrorPage("레이아웃 구성 뷰 파일 중 ".$loopViewFilePath." 파일을 찾을 수 없습니다.");
							exit;
						}
						$looplayoutCreateTime = date("YmdHis", filemtime($loopViewFilePath));									
						if($cacheCreatTime < $looplayoutCreateTime) {
							$true_count++;
						}
					}
				} else {					
					if(!$this->result_uri) {
						$this->viewHtmlErrorPage("result_uri를 지정해주세요");
						exit;									
					}
					$loopViewFilePath = $this->viewPath.$this->result_uri;
					if(!file_exists($loopViewFilePath)) {
						$this->viewHtmlErrorPage($loopViewFilePath." 파일을 찾을 수 없습니다.");
						exit;
					}
					$looplayoutCreateTime = date("YmdHis", filemtime($loopViewFilePath));									
					if($cacheCreatTime < $looplayoutCreateTime) {
						$true_count++;
					}					
				}
			}
			if($true_count > 0) {
				$result = true;
			}				
		}			
		return $result;
	}
	function writeCache($cacheData) {
		if($this->result_uri != $this->errPage) {
			if($this->useViewCache == true) {
				$this->cacheFileContent = '';
				$this->setCachePath();
				if($this->isUpdateCache() == true) {						
					$this->cacheFileContent		.= "<? /* Spac Framework View Cache Ver ".$this->ver.", Last Update ".date("YmdHis")." : http://www.phpwork.kr */ ?>\n";
					$this->cacheFileContent		.= $cacheData;
					$fp = fopen($this->cacheFilePath,"w");
					flock( $fp, LOCK_EX );
					fwrite($fp,$this->cacheFileContent);
					flock( $fp, LOCK_UN );
					fclose($fp);					
					@chmod($this->cacheFilePath,0707);					
				}					
			}				
		}
	}
	function setCachePath() {		
		$this->cacheFileName		= $this->actionClassName.".".$this->actionMethodName.".cache.php";		
		if($this->justClassPath && !is_dir($this->viewCachePath.$this->justClassPath)) {
			umask(0000);
			if(!mkdir($this->viewCachePath.$this->justClassPath, 0707)) {
				echo "[".$this->viewCachePath.$this->justClassPath."] 뷰캐시 디렉토리생성 실패";
			}
		}
		$this->cacheFilePath		= $this->viewCachePath . $this->justClassPath . '/' . $this->cacheFileName;		
	}
	function redirect() {
		$this->page_move_type	= 'redirect';
		$this->checkResultUri();
		$this->result_full_uri	= 'http://' . $_SERVER['HTTP_HOST'] . $this->result_uri;
		echo "<meta http-equiv=\"Refresh\" content=\"0; url=" . $this->result_full_uri . "\">";
		exit;
	}
	function setLayout($layoutTitle) {		
		$this->layoutTitle = $layoutTitle;
		if(!file_exists($this->iniLayoutPath)) {
			$this->NoLayoutFileError($this->iniLayoutPath);
			exit;
		}
		$conf_all_layout				= parse_ini_file($this->iniLayoutPath,true);
		$this->layout					= $conf_all_layout[$this->layoutTitle];
		$this->page_move_type			= 'dispatcherLayout';
	}
	function dispatcherLayout() {
		$this->page_move_type	= 'dispatcherLayout';
		$this->checkResultUri();
		$this->dispatcherCache();		
		if($this->viewCachePrint == false) {			
			if(!$this->targetLayoutName) {
				$this->targetLayoutName = "body";
			}			
			$layoutFileName	= array_shift($this->layout);
			$layoutFilePath = $this->viewPath . $layoutFileName;
			if(file_exists($layoutFilePath)) {
				$LayoutfileContents = file_get_contents($layoutFilePath);
			} else {
				$this->NoLayoutFileError($layoutFilePath);
				exit;
			}
			foreach($this->layout as $key => $value) {
				$loopViewFilePath = $this->viewPath.$value;
				if($key == $this->targetLayoutName) {
					if(!$this->result_uri) {
						if($value) {
							$this->result_uri = $value;
						} else {
							$this->viewHtmlErrorPage("레이아웃 환경설정파일(Layout.ini)의 '[".$this->layoutTitle."]' 레이아웃 항목 중 '".$this->targetLayoutName."' 항목의 뷰파일명을 정의하시거나 result_uri를 지정해주세요");
							exit;									
						}
					}
					$loopViewFilePath = $this->viewPath.$this->result_uri;
				}
				if(file_exists($loopViewFilePath)) {
					$InnerfileContents =  file_get_contents($loopViewFilePath);
					$LayoutfileContents = str_replace("#".$key."#", $InnerfileContents, $LayoutfileContents);
				} else {
					$this->NoLayoutFileError($loopViewFilePath);
					exit;
				}
			}
			$this->layout['layout'] = $layoutFileName;
			if($this->useTemplateTag == true) {
				$LayoutfileContents = $this->applyViewTemplate($LayoutfileContents);
			}
			if(!$this->viewDispatcherSource) {
				eval("?>".$LayoutfileContents);
			} else {
				highlight_string($LayoutfileContents,false);
			}
			if($this->useViewCache == true) $this->writeCache($LayoutfileContents);
			
			if($this->viewCacheInfo == true && $this->useViewCache == true) {
				$this->viewCacheMsg(0);
			}			
		}
	}
	function applyViewTemplate($contents) {
		$iniViewTemplateInfo = $this->getViewTemplateIni();
		if(count($iniViewTemplateInfo)) {				
			if(file_exists($iniViewTemplateInfo['ClassPath'])) {
				require_once($iniViewTemplateInfo['ClassPath']);
				if(class_exists($iniViewTemplateInfo['ClassName'])) {						
					$this->viewTemplateObject		 = new $iniViewTemplateInfo['ClassName'];					
					$this->viewTemplateObject->php_start		= $iniViewTemplateInfo['php_start'];
					$this->viewTemplateObject->php_end			= $iniViewTemplateInfo['php_end'];
					$this->viewTemplateObject->short_tag_start	= $iniViewTemplateInfo['short_tag_start'];
					$this->viewTemplateObject->short_tag_end	= $iniViewTemplateInfo['short_tag_end'];
					if(method_exists($this->viewTemplateObject,$iniViewTemplateInfo['MethodName'])) {
						$contents =  $this->viewTemplateObject->{$iniViewTemplateInfo['MethodName']}($contents);
					}
				}
			}
		}
		return $contents;
	}
	function getViewTemplateIni() {
		if(file_exists($this->iniViewTemplateFilePath)) {
			$conf_ViewTemplate			= parse_ini_file($this->iniViewTemplateFilePath);
			return $conf_ViewTemplate;
		} else {
			return array();
		}
	}
	function setAllPostVars() {
		foreach($_POST as $key => $value) {
			$this->{$key} = $value;
		}
	}
	function setAllGetVars() {
		foreach($_GET as $key => $value) {
			$this->{$key} = $value;
		}
	}
	function checkResultUri() {
		if(trim($this->result_uri)) {
			$this->result_uri = preg_replace('/^\/*/','/',$this->result_uri);
		}
	}
	function convKToU ($array_var) {
		$TmpRows = array();
		foreach($array_var as $key => $value) {
			$TmpRows[$key] = iconv("euc-kr","utf-8",$value);
		}	  
		return  $TmpRows;
	}
	function convUToK ($array_var) {
		$TmpRows = array();
		foreach($array_var as $key => $value) {
			$TmpRows[$key] = iconv("utf-8","euc-kr",$value);
		}
		return  $TmpRows;
	}
	function writeLog($fileContents,$filePath) {
		if(!file_exists($filePath)) {
			$fp = fopen($filePath,"w");
		} else {
			$fp = fopen($filePath,"a");
		}
		flock( $fp, LOCK_EX );
		fwrite($fp,$fileContents);
		flock( $fp, LOCK_UN );
		fclose($fp);
	}
	function writeSpacProcLog() {
		if($this->spacFileLoging == true) {
			$argsString 	 = print_r($this->args,true);
			$fileContents 	 = "### ------------SPAC Access Log-----------###-------------".date("Y-m-d H:i:s")."\n";
			$fileContents 	.= "1. REMOTE_ADDR 			: " . $_SERVER['REMOTE_ADDR'];
			$fileContents 	.= "\n"."2. REQUEST_URI 			: " . $_SERVER['REQUEST_URI'];
			$fileContents 	.= "\n"."3. PATH_INFO 			: " . $_SERVER['PATH_INFO'];
			$fileContents 	.= "\n"."4. QUERY_STRING 		: " . $_SERVER["QUERY_STRING"];
			$fileContents 	.= "\n"."5. Used urlPathInfo 	: " . $this->urlPathInfo;
			$fileContents 	.= "\n"."6. actionClassName 		: " . $this->actionClassName;
			$fileContents 	.= "\n"."7. actionMethodName 	: " . $this->actionMethodName;
			$fileContents 	.= "\n"."8. args 				: " . $argsString;
			$fileContents 	.= "\n";				
			$filePath		 = $this->spacLogFilePath.'/'.date("Ymd").'.access.log';
			if(!is_dir($this->spacLogFilePath)) {
				umask(0000);
				if(!mkdir($this->spacLogFilePath, 0707)) {
					echo "디렉토리생성 실패";
				}
			}
			$this->writeLog($fileContents,$filePath);				
		}			
	}
	function writeSpacErrLog($errorMsg) {
		if($this->errReport == true && $this->errFileLoging == true) {
			$argsString 	 = print_r($this->args,true);				
			$errorMsg		 = str_replace("\t","",$errorMsg);
			$fileContents 	 = "### ------------SPAC Error Log-----------###-------------".date("Y-m-d H:i:s")."\n";
			$fileContents 	.= "1. REMOTE_ADDR 			: " . $_SERVER['REMOTE_ADDR'];
			$fileContents 	.= "\n"."2. REQUEST_URI 			: " . $_SERVER['REQUEST_URI'];
			$fileContents 	.= "\n"."3. PATH_INFO 			: " . $_SERVER['PATH_INFO'];
			$fileContents 	.= "\n"."4. QUERY_STRING 		: " . $_SERVER["QUERY_STRING"];
			$fileContents 	.= "\n"."5. Used urlPathInfo 	: " . $this->urlPathInfo;
			$fileContents 	.= "\n"."6. actionClassName 		: " . $this->actionClassName;
			$fileContents 	.= "\n"."7. actionMethodName 	: " . $this->actionMethodName;
			$fileContents 	.= "\n"."8. args 				: " . $argsString;
			$fileContents 	.= "\n"."9. error Message--------------------------";
			$fileContents	.= "\n".$errorMsg;				
			$fileContents 	.= "\n\n";
			if(!is_dir($this->errLogFilePath)) {
				umask(0000);
				if(!mkdir($this->errLogFilePath, 0707)) {
					echo "디렉토리생성 실패";
				}
			}
			$filePath		 = $this->errLogFilePath.'/'.date("Ymd").'.error.log';
			if(!is_dir($this->spacLogFilePath)) {
				umask(0000);
				if(!mkdir($this->spacLogFilePath, 0707)) {
					echo "디렉토리생성 실패";
				}
			}
			$this->writeLog($fileContents,$filePath);				
		}
	}
	function checkActionClassName($class_name) {
		if(preg_match('/^[A-Z][a-zA-Z0-9_]+$/', $class_name)) {
			return true;
		} else {
			return false;
		}
	}
	function checkActionMethodName($method_name) {
		if(preg_match('/^[a-zA-Z][a-zA-Z0-9_]+$/', $method_name)) {
			return true;
		} else {
			return false;
		}
	}
	function getUrl() {
		switch($this->path_type) :
			case 1:
				$this->urlPathInfo			 = trim(stripslashes($_SERVER['PATH_INFO']));
				break;
			case 2:
				$this->urlPathInfo			 = trim(stripslashes(preg_replace('/^\/index.php/','',$_SERVER['PHP_SELF'])));
				break;
			case 3:
				$this->urlPathInfo			 = trim(stripslashes(preg_replace('/^\/index.php/','',$_SERVER['REQUEST_URI'])));
				break;
			default:
				$this->urlPathInfo			 = trim(stripslashes($_SERVER['PATH_INFO']));
		endswitch;
		if($_SERVER["QUERY_STRING"]) {				
			if($this->urlPathInfo) {
				$this->urlPathInfo 		.= "?".trim(stripslashes($_SERVER["QUERY_STRING"]));
			} else {
				$this->urlPathInfo 		= trim(stripslashes($_SERVER["QUERY_STRING"]));
			}				
		}
	}
	function checkUrlPathInfo() {
		$this->urlPathInfo = preg_replace('/^[\/]*/','/',$this->urlPathInfo);
		if($this->urlPathInfo=='/') {
			$this->urlPathInfo = $this->defaultUrlPath;
			$this->urlPathInfo = preg_replace('/^[\/]*/','/',$this->urlPathInfo);
		}
		$this->convertQueryStringVar();
	}
	function convertQueryStringVar() {
		$pattern 		= '@(&|\?)[a-zA-Z0-9_]+[=](.+)@';
		$replacement 	= '/\\2';
		$this->urlPathInfo = preg_replace($pattern,$replacement,$this->urlPathInfo);
	}
	function viewCacheMsg($Status) {
		if($Status) {
			echo "<fieldset style='font-size:12px;border-width:1;border-style:solid;border-color:green; padding:10px;margin-top:10px;color:green'>";
			echo "	<legend align='left'>ViewCache Message</legend><br/>";
			echo "		· Apply Cache : YES<br>";
			echo "		· Cache File Update : ".date("Y/m/d H:i:s", filemtime($this->cacheFilePath))."<br>";
			echo "		· Cache File Path : ".$this->cacheFilePath."</b><br>";
			echo "		· View File Path : ".$this->viewPath."<b>".$this->result_uri."</b><br>";
			echo "</fieldset>";
		} else {				
			echo "<fieldset style='font-size:12px;border-width:1;border-style:solid;border-color:orange; padding:10px;margin-top:10px;color:#FF6600'>";
			echo "	<legend align='left'>ViewCache Message</legend><br/>";
			echo "		· 캐시적용 : NO<br>";				
			if($this->result_uri == $this->errPage) {
				echo "		· 에러페이지는 캐시가 적용되지 않습니다.";
			} else {
				echo "		· Cache File Update : ".date("Y/m/d H:i:s", filemtime($this->cacheFilePath))."<br>";	
				echo "		· 생성된 캐시파일 경로 : ".$this->cacheFilePath."<br>";
				echo "		· 원본뷰파일 경로 : ".$this->viewPath."<b>".$this->result_uri."<br>";
				echo "		· 방금 캐시 파일이 업데이트 되었을 수 있습니다. 새로고침 하시면 생성된 캐시가 적용되며 캐시관련 정보가 출력됩니다.";			
			}				
			echo "</fieldset>";
		}
	}
	function goErrorPage($errMsg) {
		if($this->errFileLoging == true) {
			$this->writeSpacErrLog($errMsg);
		}
		if($this->errPrintView == true) {
			$this->viewHtmlErrorPage($errMsg);
			exit;
		} else {
			if(file_exists($this->viewPath.$this->errPage)) {					
				$this->result_uri = $this->errPage;
				$this->Dispatcher();
				exit;
			} else {					
				$this->viewHtmlErrorPage($this->viewPath.$this->errPage ." 가 존재하지 않습니다.");
				exit;					
			}				
		}			
	}
	function viewHtmlErrorPage($msgError) {
		echo "
			<html>
			<head><title>Error Report Page</title>
			<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
			<style>
				div.error {
					background-color:#FFFFCC;
					border-width:1px;
					border-style:solid;
					border-color:green;
					padding:5px;
					width:auto;
					color:blue;
					font-size:12px;
				}
				div.bottom {
					width:auto;
					padding:5px;
					text-align:right;
					color:gray;
					font-size:13px;
				}
				li {
					margin-left:10px;
				}
				.keyword {
					color:red;
					font-weight:bold;
					text-decoration:underline;
				}
			</style>
			</head>
			<body>
			<div class='error'>
				<pre>".str_replace("\t","&nbsp;",$msgError)."</pre>
			</div>
			<div class='bottom'>Spac Ver. ".$this->ver."</div>
			</body>
			</html>
		";			
	}
	function ClassNameError() {
		$msgError = "
			[클래스명 에러]
			에러명 : Class Name Error
			에러내용 : 클래스명으로 \"" .$this->actionClassName ."\" 을 사용할 수 없습니다
		";
		$this->goErrorPage($msgError);
	}
	function NoClassFileError() {
		$msgError = "
			[클래스파일 읽기에러]
			에러내용 : " . $this->actionClassFileName . " 가 없거나 읽기 실패했습니다. 
			클래스경로[모델] : " .$this->modelPath."
			안내 : " . $this->actionClassPath . " 파일을 생성해 주세요
		";
		$this->goErrorPage($msgError);			
	}
	function NoClassError() {
		$msgError = "
			[클래스 구현 에러]
			에러내용 : ".$this->actionClassPath." 파일내에 " . $this->actionClassName . " 클래스가 구현되어 있지 않습니다.. 
			클래스경로[모델] : " .$this->modelPath."
			안내 : " . $this->actionClassPath . " 파일 내에 " . $this->actionClassName . " 클래스를 구현해 주세요
		";
		$this->goErrorPage($msgError);			
	}
	function NoMethodError() {
		$msgError = "
			[메소드 구현 에러]
			에러내용 : 메소드 \"" . $this->actionMethodName . "\" 가 없습니다.</li> 
			클래스경로[모델] : " .$this->modelPath."
			안내 : " . $this->actionClassFileName . "파일의 " . $this->actionClassName . " 클래스 내에 \"" . $this->actionMethodName . "\" 메소드를 구현해 주세요</li> 
		";
		$this->goErrorPage($msgError);
	}
	function MethodNameError() {
		$msgError = "
			[메소드명 에러]
			에러내용 : 메소드명으로 " .$this->actionMethodName ." 을 사용할 수 없습니다.
		";
		$this->goErrorPage($msgError);
	}
	function NoFileError($Type) {
		$msgError = "
			[" . $Type . " 에러]
			" . $this->result_full_uri . " 페이지 를 표시할 수 없습니다. 
			파일읽기 에러 : " . $this->result_uri . " 가 없거나 읽기 실패했습니다. 
			안내 : " . $this->viewPath . $this->result_uri . " 파일을 생성해 주세요
		";
		$this->goErrorPage($msgError);
	}
	function NoLayoutFileError($filePath) {
		$msgError = "
			레이아웃 파일 읽기 에러
			" . $filePath . " 페이지 를 표시할 수 없습니다. 
			파일읽기 에러 : " . $filePath . " 가 없거나 읽기 실패했습니다.
			안내 : " . $filePath . " 파일을 생성해 주세요
		";
		$this->goErrorPage($msgError);
	}
	function NoGetInstance($filePath) {
		$msgError = "
			getInstance Error
			" . $filePath . " 페이지 를 표시할 수 없습니다.
			파일읽기 에러 : " . $filePath . " 가 없거나 읽기 실패했습니다.
			안내 : " . $filePath . " 파일을 생성해 주세요
		";
		$this->goErrorPage($msgError);
	}
	function iniControllerFileError() {
		$this->viewHtmlErrorPage("Spac 프레임워크 환경설정 파일<span style='color:green;font-weight:bold'>(\"".$this->iniControllerPath."\")</span> 이 없어 시스템을 초기화할 수 없습니다.");
		exit;
	}
	function ConfigFileError($userIniFilePath) {
		$msgError = "
			ini Config Read Error
			환경설정파일 읽기 에러 : 환경설정파일 " . $userIniFilePath . " 가 없거나 읽기 실패했습니다.
			안내 : " . $userIniFilePath . " 파일을 생성해 주시거나 읽기 권한을 설정 해 주세요.
		";
		$this->goErrorPage($msgError);
	}
	
	function NoCryptClassFileError() {
		$msgError = "
			[클래스파일 읽기에러]
			에러내용 : " . $this->cryptConfig->className . " 가 없거나 읽기 실패했습니다. 
			클래스경로 : " .$this->cryptConfig->classPath."
			안내 : " . $this->cryptConfig->classPath . " 파일을 생성해 주세요
		";
		$this->goErrorPage($msgError);			
	}
	
	function denyClassAccessError() {
		$msgError = "
			[클래스접근 에러]
			에러명 : Class Access Denied!
			에러내용 : <b>\"" . $this->actionClassName."\"</b> 클래스는 접근이 허용되어 있지 않습니다.
		";
		$this->goErrorPage($msgError);
	}
	function denyMethodAccessError() {
		$msgError = "
			[메소드접근 에러]
			에러명 : Method Access Denied!
			에러내용 : <b>\"" . $this->actionClassName."\"</b> 클래스의 <u>".$this->actionMethodName."</u> 메소드는 접근이 허용되어 있지 않습니다.
		";
		$this->goErrorPage($msgError);
	}			
	function viewGlobalVars() {
		$varText = print_r($GLOBALS,true);
		echo "
			<script>
				function ViewVars() {
					obj = document.getElementById('vars_area');
					if(obj.style.display == 'none') {
						obj.style.display = 'block';
					} else {
						obj.style.display = 'none';
					}
				}
			</script>
			<div><a href='#' onClick='ViewVars()' title='모든 페이지변수 보기'>[View Global Variable]</a></div>
			<div id='vars_area' style='display:none;background-color:#f2f2f2;padding:3px;color:blue';width:100%;height:auto;'><pre>" . $varText . "</pre></div>
		";
	}
	function viewThisObjectVars() {
		$varText = print_r($this,true);
		echo "
			<script>
				function ViewObjectVars() {
					obj = document.getElementById('object_vars_area');
					if(obj.style.display == 'none') {
						obj.style.display = 'block';
					} else {
						obj.style.display = 'none';
					}
				}
			</script>
			<div><a href='#' onClick='ViewObjectVars()' title='페이지 객체 살펴 보기'>[View This Object Variable]</a></div>
			<div id='object_vars_area' style='display:none;background-color:#f2f2f2;padding:3px;color:blue;width:100%;height:auto;'><pre>" . $varText . "</pre></div>
		";
	}
	function viewThisActionClassSource() {		
		if(file_exists($this->actionClassPath)) {			
			$actionClassFileContentsHighlight	= highlight_file($this->actionClassPath,true);
			$actionClassFileContentsHighlight	= "* <u>". $this->actionClassPath."</u>\n\n".$actionClassFileContentsHighlight; 
		}		
		echo "
			<script>
				function ViewActionClassSource() {
					obj = document.getElementById('actionclass_source_area');
					if(obj.style.display == 'none') {
						obj.style.display = 'block';
					} else {
						obj.style.display = 'none';
					}
				}
			</script>
			<div><a href='#' onClick='ViewActionClassSource()' title='액션클래스 코드 보기'>[View This ActionClass Source]</a></div>
			<div id='actionclass_source_area' style='display:none;background-color:#f2f2f2;padding:3px;color:blue;width:100%;height:auto;'>
				<pre>".$actionClassFileContentsHighlight."</pre>
			</div>
		";
	}	
	function viewThisViewFileSource() {		
		if(file_exists($this->viewPath.$this->result_uri)) {			
			$resultUriContentsHighlight	= highlight_file($this->viewPath.$this->result_uri,true);
			$resultUriContentsHighlight	= "* <u>". $this->viewPath.$this->result_uri."</u>\n\n".$resultUriContentsHighlight; 
		}		
		echo "
			<script>
				function ViewResultUriSource() {
					obj = document.getElementById('result_uri_source_area');
					if(obj.style.display == 'none') {
						obj.style.display = 'block';
					} else {
						obj.style.display = 'none';
					}
				}
			</script>
			<div><a href='#' onClick='ViewResultUriSource()' title='뷰파일 코드 보기'>[View This View File Source]</a></div>
			<div id='result_uri_source_area' style='display:none;background-color:#f2f2f2;padding:3px;color:blue;width:100%;height:auto;'>
				<pre>".$resultUriContentsHighlight."</pre>
			</div>
		";
	}	
	function ErrorBack() {
		echo "
			<script>
				alert('Call Miss');
				history.back();
			</script>
		";
		exit;
	}
	function ErrorBackMsg($msg) {
		$htmlStr = "
			<html>
			<head><title>$msg</title>
			<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
				<script>
					function Error() {
						alert('".$msg."');
						history.back();
					}
				</script>
			</head>
			<body onLoad='Error()'>
				<!-- Error Page...-->
			</body>
			</html>
		";
		echo $htmlStr;
		exit;
	}
	function ErrorMsgMove($msg, $url) {
		$htmlStr = "
			<html>
			<head><title>$msg</title>
			<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
				<script>
					function Error() {
						alert('".$msg."');
						document.location.href = '".$url."';
					}
				</script>
			</head>
			<body onLoad='Error()'>
				<!-- Error Page...-->
			</body>
			</html>
		";
		echo $htmlStr;
		exit;
	}

	function getCryptObject() {		
		$this->cryptObject = null;		
		if(file_exists($this->cryptConfig->classPath)) {			
			require_once($this->cryptConfig->classPath);
			if(class_exists($this->cryptConfig->className)) {
				$cryptObject = new $this->cryptConfig->className;
				if(method_exists($cryptObject,$this->cryptConfig->encryptMethodName) && method_exists($cryptObject,$this->cryptConfig->decryptMethodName)) {
					$this->cryptObject = $cryptObject;
				}
			}
		}
	}
	function decryptUrl() {
		if($this->useCryptUrl == true) {
			$this->urlPathInfo		= preg_replace('@^[\/]*@','',$this->urlPathInfo);
			$pattern = '@^'.$this->cryptConfig->flagFront.'(.*)'.$this->cryptConfig->flagEnd.'$@';
			if(preg_match($pattern, $this->urlPathInfo)) {
				$this->urlPathInfo	= preg_replace($pattern,'\\1',$this->urlPathInfo);
				$this->getCryptObject();
				$this->urlPathInfo	= urldecode($this->cryptObject->{$this->cryptConfig->decryptMethodName}($this->urlPathInfo));
			}
		}
	}
	function encryptUrl($url) {
		$this->getCryptObject();
		$encoded_url = $this->cryptConfig->flagFront.$this->cryptObject->{$this->cryptConfig->encryptMethodName}($url).$this->cryptConfig->flagEnd;
		$encoded_url = urlencode($encoded_url);
		$encoded_url = preg_replace('/^[\/]*/','/',$encoded_url);
		return $encoded_url;
	}
}
?>
